package servlet;

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.Java2D.Canvas;

import pasantias.Dominio.BoardBuilder;
import pasantias.Dominio.Circle;
import pasantias.Dominio.Dot;
import pasantias.Dominio.InmutableBoard;
import pasantias.Dominio.Rectangle;
import pasantias.Dominio.Square;

/**
 * Servlet implementation class BoardServlet
 */

public class BoardServlet extends HttpServlet {
	private static final String BOARD = "board";
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public BoardServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		BoardBuilder boardBuilder = getBoardBuilder(request);
		InmutableBoard board = boardBuilder.build();
		Canvas canvas = new Canvas();
		board.draw(canvas);

		BufferedImage image = canvas.toImage();

		ImageIO.write(image, "jpg", response.getOutputStream());

		response.getOutputStream().close();

	}

	private BoardBuilder getBoardBuilder(HttpServletRequest request) {
		BoardBuilder boardBuilder = (BoardBuilder) request.getSession().getAttribute(BOARD);
		
		if(boardBuilder == null){
			boardBuilder = new BoardBuilder(40, 40);
			request.getSession().setAttribute(BOARD, boardBuilder);
		}
		
		return boardBuilder;
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			IOException {

		BoardBuilder boardBuilder = getBoardBuilder(request);
		int x = Integer.parseInt(request.getParameter("x"));
		int y = Integer.parseInt(request.getParameter("y"));
		String figura = request.getParameter("figura");
		
		if (figura.equals("punto")) {
			boardBuilder.addFigure(new Dot(x,y));		
		}
		if (figura.equals("cuadrado")) {
			int size = Integer.parseInt(request.getParameter("lado"));	
			boardBuilder.addFigure(new Square(new Dot(x, y), size));
		}
		if (figura.equals("rectangulo")) {
			int width = Integer.parseInt(request.getParameter("width"));
			int height = Integer.parseInt(request.getParameter("height"));
			boardBuilder.addFigure(new Rectangle(new Dot(x, y), width, height));
		}
		if (figura.equals("circulo")) {
			int radius = Integer.parseInt(request.getParameter("radius"));
			boardBuilder.addFigure(new Circle(new Dot(x, y), radius));
		}
		
		 request.setAttribute(BOARD, boardBuilder);
	     request.getRequestDispatcher("SeleccionFigura.jsp").forward(request, response);
		
	}
}
